/**
 ******************************* ICE *******************************
 * ========================================================================
 * Project:		icetalkdataservices
 * Package:		org.ice4apps.talk.dataservices
 * File:		IceTalkDAO.java
 * Author: 		robin
 * Date: 		Feb 8, 2009
 * Description:	
 * ========================================================================
 * 
 */
package org.ice4apps.talk.dataservices;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.ice4apps.talk.dao.TalkEnterprise;
import org.ice4apps.talk.dao.TalkOffice;
import org.ice4apps.talk.dao.TalkOrganization;
import org.ice4apps.talk.dao.TalkUser;
import org.ice4apps.util.IceLog;
import org.ice4apps.util.Util;

@Stateless
public class IceTalkDataService implements IceTalkDataServiceLocal {
    @PersistenceContext(unitName = "icetalkdaoPU")
    private EntityManager entityManager;

    /*
     * (non-Javadoc)
     * @see
     * org.ice4apps.talk.dataservices.IceTalkDAOLocal#findTalkUserByExtension(java
     * .lang.String)
     */
    public TalkUser findTalkUserByExtension (String extension) {
        IceLog.info (IceTalkDataService.class, Util
                .append ("Searching user by extension: ", extension));
        final String queryString = "select extension.talkUser from Extension extension where extension.extension=:propertyValue";
        try {
            final Object obj = entityManager.createQuery (queryString)
                    .setParameter ("propertyValue", extension).getSingleResult ();
            if (obj != null)
                return (TalkUser) obj;
        } catch (Exception ex) {
            IceLog.debug (IceTalkDataService.class, ex, "No user with the give extension found");
        }
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice4apps.talk.dataservices.IceTalkDAOLocal#findTalkUserByUsernameAndDomain(java.lang.String, java.lang.String)
     */
    public TalkUser findTalkUserByUsernameAndDomain (String username, String domain) {
        final String queryString = "select talkUser from TalkUser talkUser where talkUser.username=:username and talkUser.domain=:domain";
        try {
            final Object obj = entityManager.createQuery (queryString)
                    .setParameter ("username", username).setParameter ("domain", domain).getSingleResult ();
            if (obj != null)
                return (TalkUser) obj;
        } catch (Exception ex) {
            IceLog.debug (IceTalkDataService.class, ex, "No user with the given username and domain found");
        }
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice4apps.talk.dataservices.IceTalkDataServiceLocal#addEnterprise(org.ice4apps.talk.dao.TalkEnterprise)
     */
    public void addEnterprise (TalkEnterprise enterprise) {
        entityManager.persist (enterprise);
        
    }

    /* (non-Javadoc)
     * @see org.ice4apps.talk.dataservices.IceTalkDataServiceLocal#deleteEnterprise(java.lang.String)
     */
    public void deleteEnterprise (String id) {

        
    }

    /* (non-Javadoc)
     * @see org.ice4apps.talk.dataservices.IceTalkDataServiceLocal#getEnterprise(java.lang.String)
     */
    public TalkEnterprise getEnterprise (String id) {
        return entityManager.find (TalkEnterprise.class, id);
    }

    /* (non-Javadoc)
     * @see org.ice4apps.talk.dataservices.IceTalkDataServiceLocal#addOrganization(org.ice4apps.talk.dao.TalkOrganization)
     */
    public void addOrganization (TalkOrganization org) {
        entityManager.persist (org);
    }

    /* (non-Javadoc)
     * @see org.ice4apps.talk.dataservices.IceTalkDataServiceLocal#getOrganization(java.lang.String)
     */
    public TalkOrganization getOrganization (String id) {
        return entityManager.find (TalkOrganization.class, id); 
    }

    /* (non-Javadoc)
     * @see org.ice4apps.talk.dataservices.IceTalkDataServiceLocal#addOffice(org.ice4apps.talk.dao.TalkOffice)
     */
    public void addOffice (TalkOffice office) {
        entityManager.persist (office);
    }

    /* (non-Javadoc)
     * @see org.ice4apps.talk.dataservices.IceTalkDataServiceLocal#getOffice(java.lang.String)
     */
    public TalkOffice getOffice (String id) {
        return entityManager.find (TalkOffice.class, id);
    }

    /* (non-Javadoc)
     * @see org.ice4apps.talk.dataservices.IceTalkDataServiceLocal#addUser(org.ice4apps.talk.dao.TalkUser)
     */
    public void addUser (TalkUser user) {
        entityManager.persist (user);
    }

    /* (non-Javadoc)
     * @see org.ice4apps.talk.dataservices.IceTalkDataServiceLocal#getUser(java.lang.String)
     */
    public TalkUser getUser (String id) {
        return entityManager.find (TalkUser.class, id);
    }
}
